package com.qianniu.napi.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.qianniu.napi.admin.entity.Maccount;
import com.qianniu.napi.admin.entity.MerchantApp;
import com.qianniu.napi.admin.entity.Smslog;
import com.qianniu.napi.admin.mapper.SmslogMapper;
import com.qianniu.napi.admin.service.IMaccountService;
import com.qianniu.napi.admin.service.IRedisService;
import com.qianniu.napi.admin.service.ISmslogService;
import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.util.ObjectUtil;
import com.qianniu.napi.common.util.RedisUtils;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenxuebin
 * @since 2019-09-09
 */
@Service
public class SmslogServiceImpl extends ServiceImpl<SmslogMapper, Smslog> implements ISmslogService {

    private static Logger logger = LoggerFactory.getLogger(RedisUtils.class);
    @Autowired
    private IRedisService redisService;
    @Autowired
    private IMaccountService maccountService;

    public void sendOnesms(String phone,String msg,String[] params,int templateId,Long mid,Long mpid){
        logger.info("--------------------------------------------------------------start to sendOnesms phone="+phone+" msg="+msg);

        Smslog smslog = new Smslog();
        smslog.setPhone(phone);
        smslog.setMsg(msg);
        smslog.setMid(ObjectUtil.getMid());
        smslog.setMpid(ObjectUtil.getMpid());
        MerchantApp mapp = redisService.getMApp(mpid);
        if(mapp==null||mapp.getSmsappid()==null|| com.google.common.base.Strings.isNullOrEmpty(mapp.getSmsappkey())){
            logger.info("------------------------------------sendOnetplsms mapp smsconfig null");
            return;
        }
        try {
            SmsSingleSender ssender = new SmsSingleSender(mapp.getSmsappid(), mapp.getSmsappkey());
            SmsSingleSenderResult result = ssender.send(0, "86", phone, msg, "", "");
            logger.info("----------------------------------------------------------------------------------------ssender.send result="+ JSON.toJSONString(result));
            smslog.setCreatetime(new Date());
            smslog.setState(1);
        } catch (Exception e) {
            // 网络IO错误
            logger.error("send sms failed for ",e);
            smslog.setCreatetime(new Date());
            smslog.setState(-1);
        }

        smslog.insert();
    }

    public void sendOnesmsfl(String phone,String msg,String[] params,int templateId,Long mid,Long mpid){
        logger.info("--------------------------------------------------------------start to sendOnesms phone="+phone+" msg="+msg);

        Smslog smslog = new Smslog();
        smslog.setPhone(phone);
        smslog.setMsg(msg);
        smslog.setMid(ObjectUtil.getMid());
        smslog.setMpid(ObjectUtil.getMpid());
        MerchantApp mapp = redisService.getMApp(mpid);
        if(mapp==null||mapp.getSmsappid()==null|| com.google.common.base.Strings.isNullOrEmpty(mapp.getSmsappkey())){
            logger.info("------------------------------------sendOnetplsms mapp smsconfig null");
            return;
        }
        try {
            SmsSingleSender ssender = new SmsSingleSender(mapp.getSmsappid(), mapp.getSmsappkey());
            SmsSingleSenderResult result = ssender.send(0, "86", phone, msg, "", "");
            logger.info("----------------------------------------------------------------------------------------ssender.send result="+ JSON.toJSONString(result));
            smslog.setCreatetime(new Date());
            smslog.setState(1);

            Maccount maccount = maccountService.selectOne(new EntityWrapper<Maccount>().eq("mid",smslog.getMid()));
            Maccount naccount = new Maccount();//更新短信营销费用
            naccount.setId(maccount.getId());
            naccount.setSmsmout(maccount.getSmsmout().subtract(maccount.getSmsprice()));
            naccount.setSmstotal(maccount.getSmstotal()-1);
            naccount.updateById();
        } catch (Exception e) {
            // 网络IO错误
            logger.error("send sms failed for ",e);
            smslog.setCreatetime(new Date());
            smslog.setState(-1);
        }

        smslog.insert();
    }

    public void sendOnetplsms(String phone,String msg,String[] params,int templateId,Long mid,Long mpid,Long sid,Long ywid){
        logger.info("------------------------------------sendOnetplsms come for phone="+phone);
        logger.info("------------------------------------sendOnetplsms come for msg="+msg);
        logger.info("------------------------------------sendOnetplsms come for msg.length="+msg.length());
        Smslog smslog = new Smslog();
        smslog.setPhone(phone);
        smslog.setMsg(msg);
        smslog.setMid(mid);
        smslog.setMpid(mpid);
        smslog.setSid(sid);
        smslog.setYwid(ywid);
        smslog.setSmstplid(templateId);
        String smsSign = null;// 默认签名
        MerchantApp mapp = redisService.getMApp(mpid);
        logger.info("------------------------------------sendOnetplsms mapp="+JSON.toJSONString(mapp));
        if(mapp==null||mapp.getSmsappid()==null|| com.google.common.base.Strings.isNullOrEmpty(mapp.getSmsappkey())){
            logger.info("------------------------------------sendOnetplsms mapp smsconfig null");
            smslog.setCreatetime(new Date());
            smslog.setState(-1);
            smslog.insert();
            return;
        }

        try {
            SmsSingleSender ssender = new SmsSingleSender(mapp.getSmsappid(), mapp.getSmsappkey());
            SmsSingleSenderResult result = ssender.sendWithParam("86", phone,
                    templateId, params, smsSign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
            logger.info("----------------------------------------------------------------sendOnetplsms.sendWithParam result="+ JSON.toJSONString(result));
            smslog.setCreatetime(new Date());
            smslog.setState(1);

        } catch (Exception e) {
            // 网络IO错误
            e.printStackTrace();
            logger.error("send sms failed for ",e);
            smslog.setCreatetime(new Date());
            smslog.setState(-1);
        }
        smslog.insert();
    }

    public void sendOnetplsmsjf(String phone,String msg,String[] params,int templateId,Long mid,Long mpid){
        Smslog smslog = new Smslog();
        smslog.setPhone(phone);
        smslog.setMsg(msg);
        smslog.setMid(mid);
        smslog.setMpid(mpid);
        String smsSign = null;// 默认签名
        MerchantApp mapp = redisService.getMApp(mpid);
        if(mapp==null||mapp.getSmsappid()==null|| com.google.common.base.Strings.isNullOrEmpty(mapp.getSmsappkey())){
            logger.info("------------------------------------sendOnetplsms mapp smsconfig null");
            return;
        }
        try {
            SmsSingleSender ssender = new SmsSingleSender(mapp.getSmsappid(), mapp.getSmsappkey());
            SmsSingleSenderResult result = ssender.sendWithParam("86", phone,
                    templateId, params, smsSign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信

            smslog.setCreatetime(new Date());
            smslog.setState(1);

            Maccount maccount = maccountService.selectOne(new EntityWrapper<Maccount>().eq("mid",smslog.getMid()));
            Maccount naccount = new Maccount();//更新短信营销费用
            naccount.setId(maccount.getId());
            naccount.setSmsmout(maccount.getSmsmout().subtract(maccount.getSmsprice()));
            naccount.setSmstotal(maccount.getSmstotal()-1);
            naccount.updateById();
        } catch (Exception e) {
            // 网络IO错误
            logger.error("send sms failed for ",e);
            smslog.setCreatetime(new Date());
            smslog.setState(-1);
        }
        smslog.insert();
    }

    public void sendGroupsms(){

    }

    public void sendGrouptplsms(){

    }
}
